﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using corpus_platform.Data;

namespace corpus_platform.Services
{
    public class FrameworkSearchService : ISearchService<Frame>
    {
        private Dictionary<string, Frame> frameMap = new();

        public string FilePath { get; set; }

        public void run()
        {
            string jsonString = File.ReadAllText(this.FilePath);
            List<FrameData> frames = JsonSerializer.Deserialize<List<FrameData>>(jsonString);
            foreach (FrameData frameData in frames)
            {
                Frame frame = new()
                {
                    Name = frameData.name,
                    Information = frameData.information,
                    AwakenLexis = frameData.awakenLexis,
                    FrameElements = new()
                };
                foreach (FrameElementData elementData in frameData.element)
                {
                    FrameElementType frameElementType;
                    if (elementData.container == "single")
                    {
                        if (elementData.type.Contains("bool"))
                        {
                            frameElementType = FrameElementType.Boolean;
                        }
                        else if (elementData.type.Contains("str"))
                        {
                            frameElementType = FrameElementType.String;
                        }
                        else
                        {
                            frameElementType = FrameElementType.Single;
                        }
                    }
                    else
                    {
                        frameElementType = FrameElementType.Multiple;
                    }
                    FrameElement frameElement = new()
                    {
                        Name = elementData.name,
                        Information = elementData.information,
                        IsCore = frameData.coreElement.Contains(elementData.name),
                        Type = frameElementType
                    };
                    frame.FrameElements.Add(frameElement);
                }
                this.frameMap.Add(frameData.name, frame);
            }
        }

        public List<Frame> search(string searchTerm, SearchMode searchMode)
        {
            List<Frame> frames = this.frameMap.Values.ToList<Frame>();
            return frames;
        }

    }

    internal class FrameData
    {
        public string name { get; set; }
        public string information { get; set; }
        public List<string> tag { get; set; }
        public List<string> coreElement { get; set; }
        public List<string> awakenLexis { get; set; }
        public List<FrameElementData> element { get; set; }
    }

    internal class FrameElementData
    {
        public string name { get; set; }
        public string information { get; set; }
        public string container { get; set; }
        public List<string> type { get; set; }
        public List<string> awakenLexis { get; set; }
    }
}
